<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../dbcom/CouponCom.php");
require_once(dirname(__FILE__) . "/../dbcom/ClosureCom.php");
require_once(dirname(__FILE__) . "/../dbcom/MerchantCom.php");

$merchantcom = new MerchantCom();
$couponcom = new CouponCom();
$closurecom = new ClosureCom();

$cont = $_REQUEST['cont'];
switch ($cont) {
    case 1://撤销
        $id = $_REQUEST['id'];
        $closure = $closurecom->getClosure($id);
        $coupon = $couponcom->getCoupon($closure['coupon_id']);
        $res = $couponcom->updatestatus(0, $coupon['id']);
        if ($res) {
            $res1 = $closurecom->deleteClosure($id);
            if ($res1) {
                echo json_encode(array("code" => 0, "msg" => "success"));
            } else {
                echo json_encode(array("code" => 400, "msg" => "delete closure fail"));
            }
        } else {
            echo json_encode(array("code" => 400, "msg" => "update status fail"));
        }
        break;
    case 2:
        $ids = $_REQUEST['ids'];
        $idarr = explode(",", $ids);
        if (!empty($idarr)) {
            foreach ($idarr as $v) {
                $closure = $closurecom->getClosure($v);
                $coupon = $couponcom->getCoupon($closure['coupon_id']);
                $res = $couponcom->updatestatus(0, $coupon['id']);
                if($res){
                    $res1 = $closurecom->deleteClosure($v);
                    if(!$res1){
                        echo json_encode(array("code" => 400, "msg" => "delete closure fail"));
                    }
                }else{
                    echo json_encode(array("code" => 400, "msg" => "update status fail"));
                    exit();
                }
            }
        }
        echo json_encode(array("code" => 0, "msg" => "success"));
        break;
    default :
        echo json_encode(array("code" => 500, "msg" => "undefined fail"));
        break;
}
exit();
